<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="真实姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入真实姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNo">
        <el-input v-model="queryParams.idCardNo" placeholder="请输入身份证号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['audit:audit:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['audit:audit:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['audit:audit:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['audit:audit:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="auditList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="真实姓名" width="150" align="center" prop="name" />
      <el-table-column label="身份证号" width="200" align="center" prop="idCardNo" />
      <el-table-column label="身份证人像照片" width="120" align="center" prop="frontImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.frontImg" style="width: 100" />
        </template>
      </el-table-column>
      <el-table-column label="身份证国徽照片" width="120" align="center" prop="backImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.backImg" style="width: 100" />
        </template>
      </el-table-column>
      <el-table-column label="证明资料" width="120" align="center" prop="certificationMaterial">
        <template slot-scope="scope">
          <image-preview :src="scope.row.certificationMaterial" style="width: 100" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" width="130" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <el-button :type="getAuditStatusText(scope.row.auditStatus) === '已审核' ? 'success' : 'danger'" plain>
            {{ getAuditStatusText(scope.row.auditStatus) }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="认证状态" width="150" align="center" prop="certificationStatus">
        <template slot-scope="scope">
          <el-button
            :type="getCertificationStatusText(scope.row.certificationStatus) === '认证成功' ? 'success' : getCertificationStatusText(scope.row.certificationStatus) === '认证中' ? 'info' : 'danger'"
            plain>
            {{ getCertificationStatusText(scope.row.certificationStatus) }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="驳回原因" width="180" align="center" prop="rejectReason" />
      <el-table-column label="审核人" align="center" prop="auditorName" />
      <el-table-column label="审核时间" align="center" prop="auditTime" sortable width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['audit:audit:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['audit:audit:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改服务人员认证管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务人员id" prop="serveProviderId">
          <el-input v-model="form.serveProviderId" placeholder="请输入服务人员id" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCardNo">
          <el-input v-model="form.idCardNo" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="身份证人像照片" prop="frontImg">
          <el-input v-model="form.frontImg" placeholder="请输入身份证人像照片" />
        </el-form-item>
        <el-form-item label="身份证国徽照片" prop="backImg">
          <el-input v-model="form.backImg" placeholder="请输入身份证国徽照片" />
        </el-form-item>
        <el-form-item label="证明资料" prop="certificationMaterial">
          <el-input v-model="form.certificationMaterial" placeholder="请输入证明资料" />
        </el-form-item>
        <el-form-item label="审核人id" prop="auditorId">
          <el-input v-model="form.auditorId" placeholder="请输入审核人id" />
        </el-form-item>
        <el-form-item label="审核人" prop="auditorName">
          <el-input v-model="form.auditorName" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable v-model="form.auditTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="驳回原因" prop="rejectReason">
          <el-input v-model="form.rejectReason" placeholder="请输入驳回原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addAudit, delAudit, getAudit, listAudit, updateAudit } from "@/api/audit/audit";

export default {
  name: "Audit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务人员认证管理表格数据
      auditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        idCardNo: null,
        auditStatus: null,
        certificationStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "真实姓名不能为空", trigger: "blur" }
        ],
        idCardNo: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        auditStatus: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
        certificationStatus: [
          { required: true, message: "认证状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询服务人员认证管理列表 */
    getList() {
      this.loading = true;
      listAudit(this.queryParams).then(response => {
        this.auditList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serveProviderId: null,
        name: null,
        idCardNo: null,
        frontImg: null,
        backImg: null,
        certificationMaterial: null,
        auditStatus: null,
        auditorId: null,
        auditorName: null,
        auditTime: null,
        certificationStatus: null,
        rejectReason: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务人员认证管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAudit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务人员认证管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAudit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAudit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务人员认证管理编号为"' + ids + '"的数据项？').then(function () {
        return delAudit(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('audit/audit/export', {
        ...this.queryParams
      }, `audit_${new Date().getTime()}.xlsx`)
    },
    getAuditStatusText(status) {
      switch (status) {
        case 0:
          return '未审核';
        case 1:
          return '已审核';
        default:
          return '未知状态';
      }
    },
    getCertificationStatusText(status) {
      switch (status) {
        case 1:
          return '认证中';
        case 2:
          return '认证成功';
        case 3:
          return '认证失败';
        default:
          return '未知状态';
      }
    },
  }
};
</script>
